from django.db import models

import os

# Create your models here.



class User(models.Model):
	username = models.CharField(max_length=16)
	password = models.CharField(max_length=20)
	role = models.CharField(max_length=8,choices=(('1','用户'),('2','管理员'),('3','商家')),default='用户')

# 用户信息表
# 属性	类型	说明
# id	int	主键
# user	int	用户id
# name	varchar	姓名
# gender	varchar	性别
# id_card	varchar	身份证号
# phone	varchar	手机号
# email	varchar	邮箱
# birthday	date	生日
# wallet	float	钱包
# point	int	积分

class UserInfo(models.Model):
	user = models.OneToOneField(User,on_delete=models.CASCADE)
	name = models.CharField(max_length=50,blank=True)
	gender = models.CharField(max_length=2,choices=(('1','男'),('2','女')),blank=True)
	# id_card = models.CharField(max_length=18)
	# phone = models.CharField(max_length=11)
	# email = models.CharField(max_length=50)
	birthday = models.DateField(null=True, blank=True)
	wallet = models.FloatField(default=0)
	point = models.IntegerField(default=0)

def user_directory_path(instance, filename):
    ext = filename.split('.').pop()
    # filename = '{0}{1}.{2}'.format(instance.name, instance.identity_card, ext)
    path = os.path.join('images', 'hotels', 'h'+str(instance.id) +'.'+ext) # 系统路径分隔符差异，增强代码重用性
    print('hotel url:', path)
    return path

class Hotel(models.Model):
	admin = models.ForeignKey(User,on_delete=models.CASCADE)
	name = models.CharField(max_length=50)
	level = models.CharField(max_length=1, default='1')
	location = models.CharField(max_length=50)
	# img_type = models.CharField(max_length=5)
	url = models.ImageField(upload_to=user_directory_path)
	intro = models.TextField()
	min_price = models.FloatField(default=0)

class HotelRequest(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	hotel = models.OneToOneField(Hotel,on_delete=models.CASCADE)
	status = models.CharField(max_length=6,choices=(('1','待审核'),('2','已通过'),('3','已拒绝')),default='1')

# 与酒店一对多
class Room(models.Model):
	hotel = models.ForeignKey(Hotel,on_delete=models.CASCADE)
	roomtype = models.CharField(max_length=8)  # 单人间 双人间 商务套房
	price = models.FloatField()
	rest = models.IntegerField()
	total = models.IntegerField()

class RoomRequest(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	room = models.OneToOneField(Room,on_delete=models.CASCADE)
	status = models.CharField(max_length=6,choices=(('1','待审核'),('2','已通过'),('3','已拒绝')),default='1')

# 订单
# 属性	类型	说明
# id	int	主键
# user	int	用户id
# hotel	int	酒店id
# room	int	房间id
# room_num	int	房间数
# name	varchar	住客姓名
# id_card	varchar	身份证号
# gender	varchar	性别
# phone	varchar	手机号
# email	varchar	邮箱
# price	float	价格
# checkin_date	date	入住日期
# checkout_date	date	退房日期
# order_datetime	datetime	下单时间
# pre_reachtime	datetime	预计到店时间
# status	varchar	订单状态
# note	text	备注
# stay_days	int	入住天数
	
class Order(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	hotel = models.ForeignKey(Hotel,on_delete=models.CASCADE)
	room = models.ForeignKey(Room,on_delete=models.CASCADE)
	room_num = models.IntegerField()
	name = models.CharField(max_length=20)
	id_card = models.CharField(max_length=18)
	phone = models.CharField(max_length=11)
	email = models.CharField(max_length=30,default='')
	price = models.FloatField()
	checkin_date = models.DateField()
	checkout_date = models.DateField()
	order_time = models.DateTimeField(auto_now_add=True)
	pre_reachtime = models.TimeField()
	status = models.CharField(max_length=6,choices=(('1','待支付'),('2','已预约'),('3','已取消'),('4','入住'),('5','退房'),('6','续房'),('7','已完成'),('8','已评价')), default='待支付')
	note = models.TextField(default='')
	stay_days = models.IntegerField()

class Comment(models.Model):
	order = models.OneToOneField(Order,on_delete=models.CASCADE)
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	content = models.TextField()
	# 更新时间
	time = models.DateTimeField(auto_now=True)


class CouponTemplate(models.Model):
	name = models.CharField(max_length=20)
	discount = models.FloatField()
	point_amount = models.IntegerField()
	deprecated = models.IntegerField(default=0)

# 优惠券
# 属性	类型	说明
# id	int	主键
# user	int	用户id
# name	varchar	优惠券名称
# money	float	优惠券金额
# time	datetime	优惠券有效期
# status	varchar	优惠券状态
# point	int	兑换需要的积分数量
class CouponEntity(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	coupon_template = models.ForeignKey(CouponTemplate,on_delete=models.CASCADE)
	# 兑换优惠券的时间，用于积分明细排序
	get_time = models.DateTimeField(auto_now_add=True)
	end_time = models.DateTimeField(null=True, blank=True)
	status = models.CharField(max_length=6,choices=(('1','未使用'),('2','已使用'),('3','已过期')),default='未使用')


class Pay(models.Model):
	# 可为空可不填
	user = models.ForeignKey(User,on_delete=models.CASCADE,null=True,blank=True)
	order = models.OneToOneField(Order,on_delete=models.CASCADE)
	# 可为空可不填
	coupon = models.ForeignKey(CouponEntity,on_delete=models.CASCADE,null=True,blank=True)
	pay_time = models.DateTimeField(auto_now=True)
	price = models.FloatField()
	status = models.CharField(max_length=6,choices=(('1','待支付'),('2','已支付'),('3','已取消'),('4','已退款')),default='1')
	point_amount = models.IntegerField(default=0)


class Collection(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	hotel = models.ForeignKey(Hotel,on_delete=models.CASCADE)


# 充值表
class Recharge(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	money = models.FloatField()
	time = models.DateTimeField(auto_now_add=True)
	# status = models.CharField(max_length=6,choices=(('1','待审核'),('2','已通过'),('3','已拒绝')))


# hotel activity

class HotelActivityRequest(models.Model):
	hotel = models.ForeignKey(Hotel,on_delete=models.CASCADE)
	status = models.CharField(max_length=6,choices=(('1','待审核'),('2','已通过'),('3','已拒绝'),('4','已结束')),default='待审核')

class HotelActivity(models.Model):
	hotelrequest = models.OneToOneField(HotelActivityRequest,on_delete=models.CASCADE)


class UserNumInHotelActivity(models.Model):
	hotelactivity = models.OneToOneField(HotelActivity,on_delete=models.CASCADE)
	count = models.IntegerField(default=1)

class HotelActivityUserJoin(models.Model):
	hotelactivity = models.ForeignKey(HotelActivity,on_delete=models.CASCADE)
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	join_time = models.DateTimeField(auto_now_add=True)
class Point(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	pay = models.OneToOneField(Pay,on_delete=models.CASCADE, blank=True, null=True)
	couponEntity = models.OneToOneField(CouponEntity,on_delete=models.CASCADE, blank=True, null=True)
	activity = models.OneToOneField(HotelActivityUserJoin,on_delete=models.CASCADE, blank=True, null=True)
	point_amount = models.IntegerField()
	time = models.DateTimeField(auto_now_add=True)

class News(models.Model):
	title = models.CharField(max_length=50)
	content = models.TextField()
	publish_time = models.DateTimeField()

class OrderNumByRoomType(models.Model):
	hotel = models.ForeignKey(Hotel, on_delete=models.CASCADE)
	roomtype = models.CharField(max_length=8)
	count = models.IntegerField(default=1)

class Area(models.Model):
	name = models.CharField(max_length=50)

class Checkin(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	order = models.OneToOneField(Order, on_delete=models.CASCADE)

class Checkout(models.Model):
	user = models.ForeignKey(User,on_delete=models.CASCADE)
	order = models.OneToOneField(Order,on_delete=models.CASCADE)

# class Continue(models.Model):
# 	user = models.ForeignKey(User,on_delete=models.CASCADE)
# 	order = models.OneToOneField(Order,on_delete=models.CASCADE)
# 	pay = models.OneToOneField(Pay,on_delete=models.CASCADE)
# 	days = models.IntegerField()

class UserRegisterNum(models.Model):
	day = models.DateField(auto_now_add=True)
	amount = models.IntegerField(default=1)

class PayRoute(models.Model):
	name = models.CharField(max_length=50)
	desc = models.TextField()

class DailyIncome(models.Model):
	hotel = models.ForeignKey(Hotel,on_delete=models.CASCADE)
	day = models.DateField(auto_now_add=True)
	income = models.FloatField()

class AreaOrderNum(models.Model):
	area = models.OneToOneField(Area,on_delete=models.CASCADE)
	count = models.IntegerField()


class HotelRank(models.Model):
	hotel = models.OneToOneField(Hotel,on_delete=models.CASCADE)

class HotelRecommend(models.Model):
    hotel = models.OneToOneField(Hotel,on_delete=models.CASCADE)

